// This is called with the results from from FB.getLoginStatus().

function login(){
	return(
		request = $.Deferred( function(defObj){
			FB.getLoginStatus( function( response ){
				// handle ajax response
				if(response.status !== 'connected'){
					// not logged in yet... try to do it
					FB.login(
						function( response ){
							// inside a second ajax response now (still have reference to original deferred promise thing)
							switch (response.status) {
								case 'connected':
									defObj.resolve(response);
									break;

								case 'not_authorized':
									defObj.reject( { 'msg' : "Insufficient permission!" } );
									break;

								default:
									defObj.reject( { 'msg' : "Login Failed!" } );
									break;
							}
						},
						//request these permissions
						{'scope': 'user_events,user_groups'}
					);
				}
				else{
					defObj.resolve(response);
				}
			})
		}).fail( function(e){
			// login failure handler
			document.getElementById('status').innerHTML = 'Failed to authorize user: ' + e.msg;
		}).promise()
	);
}

